package com.roadjava.tutor.handler.back.student;

import com.roadjava.tutor.bean.dto.StudentDemandDTO;
import com.roadjava.tutor.bean.res.ResultDTO;
import com.roadjava.tutor.bean.res.TableResult;
import com.roadjava.tutor.bean.vo.StudentDemandVO;
import com.roadjava.tutor.bean.vo.UserVO;
import com.roadjava.tutor.service.StudentDemandService;
import com.roadjava.tutor.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 未登录/登录 的情况下前端页面控制器
 *
 * @author zhaodaowen
 * @see <a href="http://www.roadjava.com">乐之者java</a>
 */
@Controller
@RequestMapping("/studentDemand")
public class StudentDemandHandler {
    @Resource
    private StudentDemandService studentDemandService;
    /**
     * 到管理页面
     */
    @GetMapping("/toManage")
    public String toManage() {
        return "back/student/studentDemandManage";
    }
    /**
     * 到添加求职页面
     */
    @GetMapping("/toAdd")
    public String toAdd() {
        return "back/student/addStudentDemand";
    }
    @PostMapping("/add") @ResponseBody
    public ResultDTO<String> add(StudentDemandDTO dto, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute(Constants.USER_SESSION_KEY);
        dto.setStudentId(userVO.getId());
        return studentDemandService.add(dto);
    }
    /**
     * 加载求职表格
     */
    @PostMapping("/loadTable") @ResponseBody
    public ResultDTO<TableResult<StudentDemandVO>> loadTable(StudentDemandDTO dto){
        // 根据搜索类型设置搜索词
        String searchType = dto.getSearchType();
        String searchWord = dto.getSearchWord();
        if (StringUtils.isNotBlank(searchWord)) {
            if ("title".equals(searchType)) {
                dto.setTitle(searchWord);
            }else if ("courseName".equals(searchType)) {
                dto.setCourseName(searchWord);
            }
        }
        TableResult<StudentDemandVO> tableResult=new TableResult<>();
        ResultDTO<List<StudentDemandVO>> result = studentDemandService.loadTable(dto);
        tableResult.setTotalCount(result.getTotal());
        tableResult.setRows(result.getData());
        return ResultDTO.buildSuccess(tableResult);
    }
    /**
     * 根据招聘id删除
     */
    @PostMapping("/deleteById") @ResponseBody
    public ResultDTO<String> deleteById(@RequestParam("idToDelete") Long id){
        if (id == null) {
            return ResultDTO.buildFailure("id必传");
        }
        return studentDemandService.deleteById(id);
    }
}
